package com.softcloud.gamesbase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by 喆歆 on 2015/4/21.
 */
public class HeroAdapter extends ArrayAdapter<Hero>{
    private int resourceId;

    public HeroAdapter(Context context, int textViewResourceId, List<Hero> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hero hero = getItem(position);
        View view;
        ViewHolder viewHolder = new ViewHolder();
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.hero_item,null);
            viewHolder.heroImage = (ImageView)view.findViewById(R.id.hero_image);
            viewHolder.heroName = (TextView)view.findViewById(R.id.hero_name);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.heroName.setText(hero.getHeroName());
        viewHolder.heroImage.setImageResource(hero.getHeroImageId());
        return view;
    }

    class ViewHolder{
        ImageView heroImage;
        TextView heroName;
    }
}
